import { expandIcon } from '@/utils/renderCustomFn';
import api from '@/api';
import { unref } from 'vue';
export const useTableExpand = (sonField: string, list: any, setList: (list: any[]) => void) => {
    // 动态控制展开图标
    const handleExpandIcon = (props) => {
        if (!unref(list).length) { return }
        // 从外部引入，是因为内部不支持jsx
        return expandIcon(props, sonField);
    };
    // 动态加载子数据
    const onExpandChange = async (expanded: boolean, record) => {
        if (expanded) {
            if (sonField === 'sonRequestInfos') {
                const query = {
                    id: record.id
                };
                const index = unref(list).findIndex((item) => item.id === record.id);
                const { success, moule } = await api.pmpRequestInfoModel.findByFatherId(query);
                if (success) {
                    list.value[index] = {
                        ...list.value[index],
                        children: [...moule]
                    };
                    setList(unref(list));
                }
            } else {
                const index = unref(list).findIndex((item) => item.id === record.id);
                list.value[index] = {
                    ...list.value[index],
                    children: [...list.value[index][sonField]]
                };
                setList(unref(list));
            }
        }
    };
    return {
        handleExpandIcon,
        onExpandChange
    };
};